package com.zj.core.activity

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import com.zj.core.adapter.NurseListAdapter
import com.zj.core.base.TitleBarActivity
import com.zj.core.bean.NurseRecResBean
import com.zj.core.tools.Constant
import android.support.v7.widget.LinearLayoutManager
import com.zj.core.net.NetPresenter
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.header.MaterialHeader
import com.dialogutils.ToastUtil
import com.quitutils.SwipeBackLayout
import com.zj.core.base.BaseApplication.Companion.context
import com.zj.core.bean.UserInfo
import com.zj.core.net.NetValueCallBackIml
import com.zj.core.ui.NurseListUi
import org.jetbrains.anko.setContentView


@Suppress("UNREACHABLE_CODE")
class NurseListActivity: TitleBarActivity(), NurseListAdapter.OnItemListener,
    NetValueCallBackIml {
    private var mRecordBean: NurseRecResBean.DatEntity.RowsEntity? = null
    private var mRecordAdapter: NurseListAdapter? = null
    private var mRecordBeanList: MutableList<NurseRecResBean.DatEntity.RowsEntity>? = null
    private var mPageNum = 1

    private var mOnRefush = true      //刷新状态
    private var mOnLoadMore = false    //加载更多
    private var mLayoutManager:LinearLayoutManager?=null
    private var mBundle:Bundle?=null
    private lateinit var mNurseList: NurseListUi

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setQuitMode(SwipeBackLayout.EDGE_LEFT)
    }

    override fun onCreate(groupView: LinearLayout): View?{
        mNurseList= NurseListUi(groupView)
        return mNurseList.setContentView(this)
    }

    override fun initView(){
        setTitleText("历史记录")
        mBundle=Bundle()

        mRecordBeanList = NurseRecResBean.DatEntity().rows
        mRecordBean= intent.getSerializableExtra(Constant.NURSE_LIST_KEY) as NurseRecResBean.DatEntity.RowsEntity?

        NetPresenter().getNurseOperationRecord(
            this,
            UserInfo.hospitalID,
            UserInfo.departmentID,
            mPageNum++,
            patientID = mRecordBean?.patientid.toString())

        mLayoutManager= LinearLayoutManager(this)
        mLayoutManager?.orientation = LinearLayoutManager.VERTICAL

        mRecordAdapter = NurseListAdapter(context, mRecordBeanList)
        mRecordAdapter?.setItemClickListener(this)

        with(mNurseList){
            this.list_record_view?.layoutManager = mLayoutManager
            this.list_record_view?.adapter = mRecordAdapter
            this.refreshLayout?.setRefreshHeader(MaterialHeader(this@NurseListActivity))
            this.refreshLayout?.setRefreshFooter(ClassicsFooter(this@NurseListActivity))
        }

        loadRefreshLayout()
    }

    /**
     * 初始化加载控件
     */
    private fun loadRefreshLayout(){
        mNurseList.refreshLayout?.setOnRefreshListener{
            mOnRefush = true
            mPageNum = 1
            NetPresenter().getNurseOperationRecord(
                this,
                UserInfo.hospitalID,
                UserInfo.departmentID,
                mPageNum++,
                patientID=mRecordBean?.patientid.toString()
            )
        }
        mNurseList.refreshLayout?.setOnLoadMoreListener {
            mOnLoadMore = true
            NetPresenter().getNurseOperationRecord(
                this,
                UserInfo.hospitalID,
                UserInfo.departmentID,
                mPageNum++,
                patientID=mRecordBean?.patientid.toString()
            )
        }
    }

    override fun onClick(mRecordBean: NurseRecResBean.DatEntity.RowsEntity, index: Int){
        mBundle?.putSerializable(Constant.RECORD_DETAIL_KEY,mRecordBean)
        activityJump(this, NurseDetailActivity().javaClass,mBundle)
    }

    override fun onNext(values: Any?, bean: Any?) {
        if (mOnRefush) {                        //如果正在刷新中，结束刷新
            mOnRefush = false
            mRecordBeanList?.clear()
            mRecordBeanList?.addAll((values as NurseRecResBean).dat?.rows!!)
            mNurseList.refreshLayout?.finishRefresh()
            mRecordAdapter?.onRefush((values as NurseRecResBean).dat?.rows!!)
        }else if (mOnLoadMore) {                      //加载更多中
            mOnLoadMore = false
            mRecordBeanList?.addAll((values as NurseRecResBean).dat?.rows!!)
            mNurseList.refreshLayout?.finishLoadMore()
            mRecordAdapter?.onLoadMore((values as NurseRecResBean).dat?.rows!!)
        }else{
            mRecordAdapter?.onRefush((values as NurseRecResBean).dat?.rows!!)
        }
    }

    override fun onError() {
        if (stopRefush()) {
            ToastUtil.showShort(this, "刷新失败")
        }

        if (stopLoadMore()) {
            ToastUtil.showShort(this, "加载失败")
        }
    }

    override fun onSubscribe(data: Any?) {}
    override fun onComplete(){}

    /**
     * 停止刷新
     */
    private fun stopRefush(): Boolean {
        if (mOnRefush) {                        //如果正在刷新中，结束刷新
            mOnRefush = false
            mNurseList.refreshLayout?.finishRefresh()
            return true
        }
        return false
    }

    /**
     * 停止加载更多
     *
     * @return
     */
    private fun stopLoadMore(): Boolean {
        if (mOnLoadMore) {                        //如果正在刷新中，结束刷新
            mOnLoadMore = false
            mNurseList.refreshLayout?.finishLoadMore()
            return true
        }
        return false
    }

    override fun onDestroy() {
        super.onDestroy()
        mRecordAdapter?.clean()
        if(mRecordBean!=null){
            mRecordBean=null
        }

        if(mRecordAdapter!=null){
            mRecordAdapter=null
        }
        if(mRecordBeanList!=null){
            mRecordBeanList?.clear()
            mRecordBeanList=null
        }
        if(mLayoutManager!=null){
            mLayoutManager=null
        }
    }
}